package com.young.shop.service.impl;

import com.young.shop.dao.GoodsDao;
import com.young.shop.dao.ShoppingCartDao;
import com.young.shop.dao.impl.GoodsDaoimpl;
import com.young.shop.dao.impl.ShoppingCartDaoImpl;
import com.young.shop.pojo.entity.Goods;
import com.young.shop.pojo.entity.ShoppingCart;
import com.young.shop.service.ShoppingCartService;
import com.young.shop.utils.ShoppingCartData;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author LY
 * @date 2020/12/16 10:40
 */
public class ShoppingCartServiceImpl implements ShoppingCartService {

    ShoppingCartDao shoppingCartDao = new ShoppingCartDaoImpl();
    GoodsDao goodsDao = new GoodsDaoimpl();

    //根据用户id获取购物车数据
    @Override
    public ShoppingCartData getShoppingCartDataByUserId(int id) {
        ShoppingCartData shoppingCartData = new ShoppingCartData();
        //获取指定用户的购物车信息
        List<ShoppingCart> shoppingCarts = shoppingCartDao.selectShoppingCartByUserId(id);
        shoppingCartData.setShoppingCarts(shoppingCarts);
        //获取购物车信息的商品详细信息
        List<Goods> goods = new ArrayList<>();
        for (ShoppingCart shoppingCart : shoppingCarts) {
            String pId = shoppingCart.getpId();
            Goods inf = goodsDao.getIntroductionById(pId);
            goods.add(inf);
        }
        shoppingCartData.setInformations(goods);
        return shoppingCartData;
    }

    @Override
    public ShoppingCartData updateShoppingCartNumber(int userId, String commodityId, int num) {
        //判断该商品存不存在
        int flag = shoppingCartDao.selectCountByUserIdAndCommodityId(userId, commodityId);
        if (flag == 0){
            //如果不存在则添加
            ShoppingCart shoppingCart = new ShoppingCart();
            //自动生成主键的方法
            shoppingCart.setId(Integer.parseInt(UUID.randomUUID().toString()));
            shoppingCart.setpId(commodityId);
            shoppingCart.setIsAvailable(1);
            shoppingCart.setuId(userId);
            shoppingCart.setNumber(num);
            int i = shoppingCartDao.insertShoppingCart(shoppingCart);
            return getShoppingCartDataByUserId(userId);
        }
        int number = shoppingCartDao.selectNumberByUserIdAndCommodityId(userId, commodityId);
        number += num;
        if(number<=0){
            int i = shoppingCartDao.deleteShoppingCart(userId, commodityId);
        }else {
            shoppingCartDao.updateShoppingCartNumber(userId,commodityId,number);
        }
        return getShoppingCartDataByUserId(userId);
    }

    @Override
    public ShoppingCartData clearShoppingCartByUserId(int userId, List<String> commodityIds) {
        if(commodityIds != null){
            for (String commodityId : commodityIds) {
                shoppingCartDao.deleteShoppingCart(userId,commodityId);
            }
        }
        return getShoppingCartDataByUserId(userId);
    }

    @Override
    public ShoppingCartData addShoppingCart(int userId, String commodityId) {
        return null;
    }
}
